package com.xiangxiao.rpan.authority.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.netflix.discovery.converters.Auto;
import com.xiangxiao.rpan.authority.constant.GlobalConsts;
import com.xiangxiao.rpan.authority.constant.ResponseCode;
import com.xiangxiao.rpan.authority.constant.ResponseMessage;
import com.xiangxiao.rpan.authority.consumer.IConsumerSettingsClient;
import com.xiangxiao.rpan.authority.dto.JsonObject;
import com.xiangxiao.rpan.authority.exception.LoginException;
import com.xiangxiao.rpan.authority.service.ImgVerifyCodeService;
import com.xiangxiao.rpan.authority.service.LoginService;
import com.xiangxiao.rpan.authority.vo.LoginVerifyImgVo;
import com.xiangxiao.rpan.authority.vo.TopicVo;
import com.xiangxiao.rpan.authority.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.apache.commons.lang.StringUtils;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/7/4 14:45
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class LoginController {
  private static Logger logger = LoggerFactory.getLogger(LoginController.class);
  @Autowired
  private ImgVerifyCodeService imgVerifyCodeService;
  @Autowired
  private LoginService loginService;
  @Autowired
  private IConsumerSettingsClient consumerSettingsClient;

  @GetMapping("/login/captcha")
  public JsonObject loginCaptcha() {
    try {
      LoginVerifyImgVo vo = imgVerifyCodeService.generatorCaptcha();
      return new JsonObject(ResponseMessage.GENERATE_VERIFY_CODE_SUECCESS, ResponseCode.RESULT_SUCCESS, vo);
    } catch (LoginException e) {
      logger.error("生成图形验证码失败", e);
      return new JsonObject(ResponseMessage.GENERATE_VERIFY_CODE_FAILED, ResponseCode.RESULT_FAIL);
    }
  }

  @GetMapping("/login/verify-mode")
  public JsonObject loginVerifyMode() {
    try {
      JSONObject resp = consumerSettingsClient.loginVerifyMode();
      String mode = resp.getObject("data", new TypeReference<String>() {
      });
      return new JsonObject(ResponseMessage.QUERY_LOGIN_VERIFY_MODE_SUECCESS, ResponseCode.RESULT_SUCCESS, null, mode);
    } catch (LoginException e) {
      logger.error("生成图形验证码失败", e);
      return new JsonObject(ResponseMessage.QUERY_LOGIN_VERIFY_MODE_FAILED, ResponseCode.RESULT_FAIL);
    }
  }

  @GetMapping("/login/check")
  public JsonObject loginCheck() {
    try {
      boolean isLogined = loginService.queryLoggedStatus();
      return new JsonObject(ResponseMessage.CHECK_LOGIN_STATUS_SUECCESS, ResponseCode.RESULT_SUCCESS, isLogined);
    } catch (LoginException e) {
      logger.error("检查登录状态失败", e);
      return new JsonObject(ResponseMessage.CHECK_LOGIN_STATUS_FAILED, ResponseCode.RESULT_FAIL);
    }
  }

  @GetMapping("/usersList")
  public JsonObject getAllUsers() {
    try {
      List<UserVo> vos = loginService.queryAllUsers();
      return new JsonObject(ResponseMessage.QUERY_ALL_USERS_SUECCESS, ResponseCode.RESULT_SUCCESS, vos);
    } catch (LoginException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.QUERY_ALL_USERS_FAILED, ResponseCode.QUERY_ALL_USERS_FAILED);
    }
  }

  @PostMapping("/logout")
  public JsonObject logout(HttpServletRequest request) {
    try {
      String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
      if (StringUtils.isBlank(authorization) || !authorization.contains(GlobalConsts.AUTHORIZATION_BEARER)) {
        return new JsonObject(ResponseMessage.AUTHENTICATION_DEFECT, ResponseCode.LOGOUT_FAILED);
      }
      String token = authorization.substring(GlobalConsts.AUTHORIZATION_BEARER.length() + 1);
      if (StringUtils.isBlank(token)) {
        return new JsonObject(ResponseMessage.LOGOUT_FAIL, ResponseCode.LOGOUT_FAILED);
      }
      loginService.logout(token);
      return new JsonObject(ResponseMessage.LOGOUT_SUECCESS, ResponseCode.RESULT_SUCCESS);
    } catch (LoginException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.LOGOUT_FAIL, ResponseCode.LOGOUT_FAILED);
    }
  }
}
